package com.unohacha.qqj.fzdr;

import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.qpg.widget.common.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.unohacha.qqj.fzdr.common.bean.GongYingBean;
import com.unohacha.qqj.fzdr.common.bean.HttpResult;
import com.unohacha.qqj.fzdr.common.bean.SupplyADemandBean;
import com.unohacha.qqj.fzdr.common.http.RetrofitManager;
import com.unohacha.qqj.fzdr.main.CommonSDActivity;
import com.unohacha.qqj.fzdr.ui.activity.supply.FabricCateActivity;
import com.unohacha.qqj.fzdr.ui.activity.supply.FindJobActivity;
import com.unohacha.qqj.fzdr.util.Prefer;
import com.unohacha.qqj.fzdr.util.SpaceItemDecorationFore;
import com.unohacha.qqj.mylibrary.base.BaseActivity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class SupplyDemandTypeActivity1 extends BaseActivity implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.common_back)
    ImageView commonBack;
    @BindView(R.id.common_title)
    TextView commonTitle;
    @BindView(R.id.common_right_image)
    ImageView commonRightImage;
    //    @BindView(R.id.image_grid_layout)
//    MyGridView imageGridLayout;
    @BindView(R.id.mRefreshLayout)
    SmartRefreshLayout mRefreshLayout;
    @BindView(R.id.recycleview)
    RecyclerView recycleview;
    private String type;
    private int a;
    private List<GongYingBean> list;
    private List<SupplyADemandBean.DataBean> dataBeanList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_supply_demand;
    }

    @Override
    protected void initView() {
        super.initView();
        list = new ArrayList<>();
        commonRightImage.setVisibility(View.INVISIBLE);
//        imageGridLayout.setOnItemClickListener(this);
        WindowManager manager = SupplyDemandTypeActivity1.this.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        a = outMetrics.widthPixels - 50;
        GridLayoutManager layoutManage = new GridLayoutManager(this, 4);
        recycleview.setLayoutManager(layoutManage);
        recycleview.addItemDecoration(new SpaceItemDecorationFore(20));
        type = this.getIntent().getStringExtra("type");
        if (type.equals("1")) {
            commonTitle.setText("供应");
        } else if (type.equals("2")) {
            commonTitle.setText("需求");
        }else {
            commonTitle.setText("更多供求");
            type = "1";
        }
        
        initMessage(type);
       
    }

    @OnClick(R.id.common_back)
    public void onViewClicked() {
        finish();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        switch (list.get(position).getName()) {
            case "纺织面料":
                Intent intent1=new Intent(SupplyDemandTypeActivity1.this, CommonSDActivity.class);
//                intent1.putExtra("main_id",list.get(position).getId()+"");
//                intent1.putExtra("type",type);
//                startActivity(intent1);
                Prefer.getInstance().setEcosphere(1);
                intent1.putExtra("supply_cate_id",list.get(position).getId());
                intent1.putExtra("title",list.get(position).getName());
                intent1.putExtra("recommended_type","1");
                startActivity(intent1);
                break;
            case "纺织织造":
                Prefer.getInstance().setEcosphere(2);
                Intent intent = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent.putExtra("supply_cate_id",list.get(position).getId());
                intent.putExtra("member_id","");
                intent.putExtra("title",list.get(position).getName());
//                intent.putExtra("type",type);
                intent.putExtra("recommended_type","1");
                startActivity(intent);
                break;
            case "纺织库存":
                Prefer.getInstance().setEcosphere(9);
                Intent intent2 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent2.putExtra("supply_cate_id",list.get(position).getId());
                intent2.putExtra("member_id","");
                intent2.putExtra("title",list.get(position).getName());
                intent2.putExtra("recommended_type","1");
                startActivity(intent2);
                break;

            case "设备维修":
                Prefer.getInstance().setEcosphere(8);
                Intent intent3 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent3.putExtra("supply_cate_id",list.get(position).getId());
                intent3.putExtra("member_id","");
                intent3.putExtra("title",list.get(position).getName());
//                intent3.putExtra("type",type);
                intent3.putExtra("recommended_type","1");
                startActivity(intent3);
                break;

            case "人才招聘":
                Prefer.getInstance().setEcosphere(4);
                Intent intent4 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent4.putExtra("supply_cate_id",list.get(position).getId());
                intent4.putExtra("member_id","");
                intent4.putExtra("title",list.get(position).getName());
//                intent4.putExtra("type",type);
                intent4.putExtra("recommended_type","1");
                startActivity(intent4);
                break;

//            case "纺织原料":
//                Intent intent5=new Intent(SupplyDemandTypeActivity.this, RawMaterialActivity.class);
//                intent5.putExtra("id",list.get(position).getId()+"");
//                intent5.putExtra("type",type);
//                startActivity(intent5);
//                // jumpToOtherActivity(this, ZhizaoActivity.class);
//                break;

            case "人员求职":
                Intent intent6=new Intent(SupplyDemandTypeActivity1.this, FindJobActivity.class);
                intent6.putExtra("id",list.get(position).getId()+"");
                intent6.putExtra("type",type);
                startActivity(intent6);
                // jumpToOtherActivity(this, ZhizaoActivity.class);
                break;
//            case "房屋租赁":
//                Intent intent7=new Intent(SupplyDemandTypeActivity.this, LeaseOfHousesActivity.class);
//                intent7.putExtra("id",list.get(position).getId()+"");
//                intent7.putExtra("type",type);
//                startActivity(intent7);
//                // jumpToOtherActivity(this, ZhizaoActivity.class);
//                break;
//            case "纺织设备":
//                Intent intent8=new Intent(SupplyDemandTypeActivity.this, TextileEquipmentActivity.class);
//                intent8.putExtra("id",list.get(position).getId()+"");
//                intent8.putExtra("type",type);
//                startActivity(intent8);
//                // jumpToOtherActivity(this, ZhizaoActivity.class);
//                break;
            case "纺织原料":
                Prefer.getInstance().setEcosphere(3);
                Intent intent5 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent5.putExtra("supply_cate_id",list.get(position).getId());
                intent5.putExtra("member_id","");
                intent5.putExtra("title",list.get(position).getName());
                intent5.putExtra("recommended_type","1");
                startActivity(intent5);
                break;
            case "其他供求":
                Prefer.getInstance().setEcosphere(12);
                Intent intent12 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent12.putExtra("supply_cate_id",list.get(position).getId());
                intent12.putExtra("member_id","");
                intent12.putExtra("title",list.get(position).getName());
                intent12.putExtra("recommended_type","1");
//                startActivity(intent12);
                ToastUtil.showCustomToast("敬请期待");
                break;
            case "房屋租赁":
                Prefer.getInstance().setEcosphere(6);
                Intent intent7 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent7.putExtra("supply_cate_id",list.get(position).getId());
                intent7.putExtra("member_id","");
                intent7.putExtra("title",list.get(position).getName());
                intent7.putExtra("recommended_type","1");
                startActivity(intent7);
//                ToastUtil.showCustomToast("敬请期待");
                break;
            case "纺织设备":
                Prefer.getInstance().setEcosphere(7);
                Intent intent8 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent8.putExtra("supply_cate_id",list.get(position).getId());
                intent8.putExtra("member_id","");
                intent8.putExtra("title",list.get(position).getName());
                intent8.putExtra("recommended_type","1");
                startActivity(intent8);
//                ToastUtil.showCustomToast("敬请期待");
                break;
            case "后道处理":
                Prefer.getInstance().setEcosphere(11);
                Intent intent9 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent9.putExtra("supply_cate_id",list.get(position).getId());
                intent9.putExtra("member_id","");
                intent9.putExtra("title",list.get(position).getName());
                intent9.putExtra("recommended_type","1");
                startActivity(intent9);
//                ToastUtil.showCustomToast("敬请期待");
                break;

            case "纺织辅料":
                Prefer.getInstance().setEcosphere(10);
                Intent intent11 = new Intent(SupplyDemandTypeActivity1.this,CommonSDActivity.class);
                intent11.putExtra("supply_cate_id",list.get(position).getId());
                intent11.putExtra("member_id","");
                intent11.putExtra("title",list.get(position).getName());
                intent11.putExtra("recommended_type","1");
                startActivity(intent11);
                break;

        }
    }

    private class MyAdapter extends BaseQuickAdapter<GongYingBean, BaseViewHolder> {

        public MyAdapter(int layoutResId, @Nullable List<GongYingBean> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, GongYingBean item) {
            ImageView imageView = (ImageView) helper.getView(R.id.supply_item_type_image);
            imageView.setMinimumWidth(a / 4);
            imageView.setMinimumHeight(a / 4);
            Glide.with(SupplyDemandTypeActivity1.this)
                    .load(item.getThumb())
                    .error(R.mipmap.ic_default_photo)
                    .override(a / 4, a / 4)
                    .into(imageView);
            helper.setText(R.id.supply_item_type_text, item.getName());
        }
    }

    // 加载发布数据
    private void initMessage(String type) {
        // 测试数据
        showLoading();
        System.out.println("------->type" + type);
        RetrofitManager.getInstance()
                .GongYing(Prefer.getInstance().getToken(), type)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Action1<HttpResult<List<GongYingBean>>>() {
                    @Override
                    public void call(HttpResult<List<GongYingBean>> gongYingBeanHttpResult) {
                        list.clear();
                        list.addAll(gongYingBeanHttpResult.getData());
                        MyAdapter adapter = new MyAdapter(R.layout.activity_supply_demand_grid_item, gongYingBeanHttpResult.getData());
                        adapter.setOnItemClickListener(SupplyDemandTypeActivity1.this);
                        recycleview.setAdapter(adapter);
                        if (mRefreshLayout.isRefreshing()) {
                            mRefreshLayout.finishRefresh();
                        }
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {

                    }
                });
        hideLoading();
    }
}